package dyyx;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import dyyx.util.EsClientUtils;
import dyyx.util.FileUtil;

public class EsCityIndex {
	
	
	
	public static void main(String[] args) throws Exception {  
		
		String url = "http://127.0.0.1:9200/citys/city";
		String file = "/Users/dugang/work/iview/data/city-data.json";
		String str = FileUtil.readString(file, null);
		str = str.trim();
		JSONArray arr = JSON.parseArray(str);
		System.out.println(arr.size());
		int id = 0;
		int num = arr.size();
		for(int i=0;i<num;i++){
			id++;
			JSONObject obj = arr.getJSONObject(i);
			Map map = new HashMap();
			map.putAll(obj);
			map.put("id", id);
			
			String lat = obj.getString("lat");
			String lng = obj.getString("lng");

			Map location = new HashMap();
			location.put("lat", lat);
			location.put("lon", lng);

			map.remove("lat");
			map.remove("lng");
			
			map.put("location", location);

			
			String result = EsClientUtils.doExecute(url+"/"+id, "PUT", JSON.toJSONString(map), null);
			
			if(i>0&i%100 ==0){
				System.out.println(i+","+LocalDateTime.now()+","+result);

			}
			
		}
		System.out.println("index done "+LocalDateTime.now());

		


	}
	
}
